package com.shg.web.mail.action;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.shg.bean.Users;
import com.shg.service.UsersService;
import com.shg.web.tools.MD5;
import com.shg.web.tools.TimerTool;



/**
 * 找回密码
 * @author Administrator
 *
 */
@Controller
public class RetrievePassWordAction extends MailAction  implements MailInterface{

	//接受者邮箱
	private String to;
	//邮箱的内容
	private String content;
	//验证码
	private int validate=0;
	//页面穿过来的验证吗
	private int check;
	//用户
	private Users user;
	//
	private String message;
	//用户服务
	@Resource UsersService usersService;
	
    public String show(){
    	return "show";
    }
    /**
     * 发送邮件
     */
	@Override
	public String validate() {
		validate = (int) (Math.random()*99999);
		//定时删除
		new TimerTool(this);
		sendMail(to, "修改密码", "尊敬的：\n"+
								"您好！\n"+
								"您的密码修改验证码是："+validate+
								"\n本邮件由系统自动发送，请勿回复!");
		this.message="验证码已经发到你的邮箱，请登录你的邮箱获取验证码";
		return "validate";
	}
	/**
	 * 发送
	 * @return
	 */
	public String passWord(){
		if(validate==check){
			user.setPassWord(MD5.MD5Encode(user.getPassWord()));
			try {
				usersService.updatePassWord(user);
			} catch (Exception e) {
				e.printStackTrace();
				this.message="操作发生异常";
				return "error";
			}
			return "success";
		}
		return "failure";
	}
	public String getTo() {
		return to;
	}

	public void setTo(String to) {
		this.to = to;
	}
	
	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Users getUser() {
		return user;
	}


	public void setUser(Users user) {
		this.user = user;
	}


	public int getCheck() {
		return check;
	}


	public void setCheck(int check) {
		this.check = check;
	}
	public void updateValidate(int validate) {
		this.validate=validate;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
}
